package com.google.code.gfcalendar;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 *
 * @author mike
 */
public class GfCalendar
{
  private final Calendar calendar = GregorianCalendar.getInstance();
  private DateMY currentMonth;

  public GfCalendar(int month, int year)
  {
    currentMonth = new DateMY(month, year);
    calendar.set(year, month, 1);
    while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY)
      calendar.add(Calendar.DATE, -1);
  }

  public DateMY getCurrentMonth()
  {
    return currentMonth;
  }

  public boolean hasNext()
  {
    boolean result = !((calendar.get(Calendar.MONTH) != currentMonth.getMonth()) &&
      (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY));
    if (result)
      calendar.add(Calendar.DATE, 1);
    return result;
  }

  public boolean isOtherMonth()
  {
    return (calendar.get(Calendar.MONTH) != currentMonth.getMonth());
  }

  public int getDate()
  {
    return calendar.get(Calendar.DATE);
  }

  public boolean isWeekEnd()
  {
    return (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) ||
     (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
  }

  public class DateMY
  {
    private int month;
    private int year;

    public DateMY(int month, int year)
    {
      this.month = month;
      this.year = year;
    }

    public int getMonth()
    {
      return month;
    }

    public int getYear()
    {
      return year;
    }

    public DateMY getNextMonth()
    {
      int nextYear = year;
      int nextMonth = month + 1;
      if (nextMonth == 12)
      {
        nextMonth = 0;
        nextYear = nextYear + 1;
      }
      return new DateMY(nextMonth, nextYear);
    }

    public DateMY getPreviousMonth()
    {
      int nextYear = year;
      int nextMonth = month - 1;
      if (nextMonth == -1)
      {
        nextMonth = 11;
        nextYear = nextYear - 1;
      }
      return new DateMY(nextMonth, nextYear);
    }
  }
}
